package com.java1412.web.utils;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.java1412.common.utils.ConstantUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Set;
import java.util.UUID;

/**
 * 发送短信的工具类
 *
 * @author java1412
 * @since 2025-05-16 16:10
 */
@Component
public class SmsUtil {

    /**
     * 发送四位验证码
     *
     * @param phone
     * @param reqId
     * @param code
     */
    public void send4Code(String reqId, String phone, String code) {
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "8a216da87dc23fe1017de0bce5f0081e";
        String accountToken = "0be5364e10a64d10b80ef17792b3e9d2";
        //请使用管理控制台中已创建应用的APPID
        String appId = "8a216da87dc23fe1017de0bce6fc0824";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        // String to = "15005176179";
        String templateId = "1";
        String[] datas = {code, ConstantUtil.CODE_TIME_OUT + "分钟", "变量3"};
        String subAppend = "1234";  //可选 扩展码，四位数字 0~9999
        //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        // String reqId = UUID.randomUUID().toString().replaceAll("-", "");
        //HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
        HashMap<String, Object> result = sdk.sendTemplateSMS(phone, templateId, datas, subAppend, reqId);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
        } else {
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
        }
    }
}
